<?php
// $Id: waimai.php 209 2012-03-14 14:25:31Z tomqin@gmail.com $

class Waimai_Controller extends FrontController{

    var $params = array();

    function init(){
        $this->zone = App::loadModule('zone');
        $this->zone->initCity();
        $this->setVar('zone', $this->zone);
    }

    function _onDefault(){
        $this->setVar('action', $this->action);
        $currZone = $this->zone->getZoneByPy($this->action);
        if($currZone->parent_id == 0){
            $selBigZoneId = $currZone->zone_id;
            $selSubZoneId = 0;
        }else{
            $selBigZoneId = $currZone->parent_id;
            $selSubZoneId = $currZone->zone_id;
        }

        $pagesize = 10;
        $params = $this->parseParam();

        $query  = str_replace(array('"',"%","&","'"), '', $_REQUEST['q']);
        $page   = $_REQUEST['page'] = $params['p'];
        $result = $this->search($selBigZoneId, $selSubZoneId, $page, $pagesize, $query);
        $total  = $result[0][0]['total'];

        $pager  = App::loadLibrary('Pager', array($total, $pagesize));
        $urltpl = App::frontUrl($this->controller, $this->action, 'p{PAGE}?q='.urlencode($query));

        $bigzone  = $this->zone->getZoneById($selBigZoneId)->name;
        $subzone  = $this->zone->getZoneById($selSubZoneId)->name;
        $title   = "大连{$bigzone}外卖信息_大连{$bigzone}{$subzone}快餐外卖送餐_网上订餐";

        $this->seoTitle($title);
        $this->setVar('pagestring', $pager->frontPageLink($urltpl));
        $this->setVar('result', $result[0]);
        $this->setVar('query', $query);
        $this->setVar('big_zone_id', $selBigZoneId);
        $this->setVar('sub_zone_id', $selSubZoneId);
        $this->render('waimai-search');
    }

    function search($big_zone_id, $sub_zone_id, $page, $pagesize = 5, $query = ''){
        $big_zone_id = intval($big_zone_id);
        $sub_zone_id = intval($sub_zone_id);

        $result = Db::proc("call sp_search_store({$pagesize}, {$page}, {$big_zone_id}, {$sub_zone_id}, '{$query}')");
        return $result;
    }

    private function parseParam(){
        $param = explode('-', $this->param);
        $array = array();
        foreach($param as $var){
            $array[substr($var, 0, 1)] = substr($var, 1);
        }
        $array['p'] = array_get_int($array, 'p', 1);
        return $array;
    }

    function __destruct(){
        //print_r($this->db);
    }
}
?>